//
//  JourneyNoticeViewController.m
//  MyTravel
//
//  Created by Hunk on 11-5-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "JourneyNoticeViewController.h"
#import "NetworkManager.h"
#import "MyLoadingView.h"
#import <QuartzCore/QuartzCore.h>

#define TAG_TABLEVIEW 14
#define TAG_SEGMENTEDCONTROL 33

@implementation JourneyNoticeViewController
@synthesize journeyId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(arrayNotice)
    SAFE_RELEASE(arrayAllContent)
    SAFE_RELEASE(noticeTextView)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:@"参团须知"];
    // Background view
    //self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // Init Notice array
    arrayNotice = [[NSMutableArray alloc] init];
    arrayAllContent = [[NSMutableArray alloc] init];
    
    /*
    // Add tableView
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, 320, 416 - 30)
                                                          style:UITableViewStyleGrouped];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setTag:TAG_TABLEVIEW];
    [self.view addSubview:tableView];
    [tableView release];
    */
    // Background view
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 320, 416-40)];
    [bgImageView setImage:[[UIImage imageNamed:@"CellBackground.png"]
                           stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    // Border view
    UIView* borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 320, 416-40)];
    [borderView setBackgroundColor:[UIColor clearColor]];
    borderView.layer.borderWidth = 2.0;
    borderView.layer.borderColor = [UIColor brownColor].CGColor;
    [self.view addSubview:borderView];
    [borderView release];
    
    // TextView
    noticeTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 416-40)];
    [noticeTextView setBackgroundColor:[UIColor clearColor]];
    [noticeTextView setShowsVerticalScrollIndicator:YES];
    [noticeTextView setFont:[UIFont fontWithName:@"AmericanTypewriter" size:18]];
    //[noticeTextView setTextColor:[UIColor brownColor]];
    [noticeTextView setTextColor:TEXTCOLOR]; 
    [noticeTextView setTextAlignment:UITextAlignmentLeft];
    [noticeTextView setEditable:NO];
    [noticeTextView setText:@""];
    [self.view addSubview:noticeTextView];
    
    
    // toolbar
    UIToolbar* toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
    [toolbar setBackgroundColor:[UIColor clearColor]];
    [toolbar setBarStyle:UIBarStyleDefault];
    [toolbar setTintColor:TINTCOLOR];
    [self.view addSubview:toolbar];
    
    // SegmentedControl
    UISegmentedControl* segmentedControl = [[[UISegmentedControl alloc] initWithItems:
                                             [NSArray arrayWithObjects:@"费用包含", @"费用不包含", @"温馨提示", nil]] autorelease];
    [segmentedControl setFrame:CGRectMake(10, 5, 300, 30)];
    [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentedControl addTarget:self 
                         action:@selector(segmentedControlHandle:) 
               forControlEvents:UIControlEventValueChanged];
    [segmentedControl setTag:TAG_SEGMENTEDCONTROL];
    [segmentedControl setTintColor:TINTCOLOR];
    [toolbar addSubview:segmentedControl];
    [segmentedControl setSelectedSegmentIndex:0];
    
    // Data
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:journeyId, @"journeyId", nil];
    
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetJourneyNotice
                                                   WithParameters:parameters
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
    
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
}

#pragma mark -
#pragma mark segmentedControlHandle
- (void)segmentedControlHandle:(id)sender
{
    UISegmentedControl* segmentedControl = (UISegmentedControl*)sender;
    
    //UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
    
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
        {
            if(0 != [arrayNotice count])
            {
                [noticeTextView setText:[arrayNotice objectAtIndex:0]];
//                [arrayNotice removeAllObjects];
//                [arrayNotice addObjectsFromArray:[arrayAllContent objectAtIndex:0]];
//                [tableView reloadData];
            }
        }
            break;
        case 1:
        {
            [noticeTextView setText:[arrayNotice objectAtIndex:1]];
//            [arrayNotice removeAllObjects];
//            [arrayNotice addObjectsFromArray:[arrayAllContent objectAtIndex:1]];
//            [tableView reloadData];
        }
            break;
        case 2:
        {
            [noticeTextView setText:[arrayNotice objectAtIndex:2]];
//            [arrayNotice removeAllObjects];
//            [arrayNotice addObjectsFromArray:[arrayAllContent objectAtIndex:2]];
//            [tableView reloadData];
        }
            break;
        default:
            break;
    }
}

/*
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [arrayNotice count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{	
    NSString* textString;
    UISegmentedControl* segmentedControl = (UISegmentedControl*)[self.view viewWithTag:TAG_SEGMENTEDCONTROL];
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        case 1:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        case 2:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    
    CGSize size = [textString sizeWithFont:[UIFont systemFontOfSize:16]
                                                        constrainedToSize:CGSizeMake(300, 1000)
                                                            lineBreakMode:UILineBreakModeCharacterWrap];
    return (size.height > 50.0 ? size.height + 20 : 50.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];
	cell.accessoryType = UITableViewCellAccessoryNone;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
	cell.textLabel.numberOfLines = 30;
    NSString* textString;
    
    UISegmentedControl* segmentedControl = (UISegmentedControl*)[self.view viewWithTag:TAG_SEGMENTEDCONTROL];
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        case 1:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        case 2:
            textString = [arrayNotice objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d.%@", (indexPath.row + 1), textString];
    
    return cell;
}
*/

#pragma mark -
#pragma mark NetworkManager callback Methods
- (void)onSuccess:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
//    NSArray* includeFeeArray = [[resultDic objectForKey:@"journeyIncludeFee"] componentsSeparatedByString:@"[OVER]"];
//    NSArray* notIncludeFeeArray = [[resultDic objectForKey:@"journeyNotIncludeFee"]  componentsSeparatedByString:@"[OVER]"];
//    NSArray* warmTipsArray = [[resultDic objectForKey:@"journeyWarmTips"]  componentsSeparatedByString:@"[OVER]"];
    
    NSString* includeFeeString = [resultDic objectForKey:@"journeyIncludeFee"];
    NSString* notIncludeFeeString = [resultDic objectForKey:@"journeyNotIncludeFee"];
    NSString* warmTipsString = [resultDic objectForKey:@"journeyWarmTips"];
    
    [arrayNotice removeAllObjects];
    [arrayNotice addObject:includeFeeString];
    [arrayNotice addObject:notIncludeFeeString];
    [arrayNotice addObject:warmTipsString];
    
//    [arrayNotice removeAllObjects];
//    [arrayNotice addObjectsFromArray:[arrayAllContent objectAtIndex:0]];
    
    [noticeTextView setText:includeFeeString];
    
//    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
//	[tableView reloadData];
}

- (void)onFailed:(id)sender
{
	NSDictionary* resultDic = (NSDictionary*)sender;
    
    // 暂无数据提示
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:SORRY_NODATA 
                                                        message:[resultDic objectForKey:@"message"]
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark scrollViewDidScroll 上拉刷新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
